package com.shanshan.manage.web.utils;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeTableOcrRequest;
import com.aliyun.ocr_api20210707.models.RecognizeTableOcrResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.shanshan.manage.common.annotation.Anonymous;
import com.shanshan.manage.web.core.config.AliYunConfig;
import com.shanshan.manage.web.domain.ACustomsMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

@Configuration
public class RecognizeTableOcr {

    @Autowired
    private AliYunConfig aliYunConfig;


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }

    /**
     * 使用STS鉴权方式初始化账号Client，推荐此方式。
     * @param accessKeyId
     * @param accessKeySecret
     * @param securityToken
     * @return Client
     * @throws Exception
     */
    public static Client createClientWithSTS(String accessKeyId, String accessKeySecret, String securityToken) throws Exception {
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret)
                // 必填，您的 Security Token
                .setSecurityToken(securityToken)
                // 必填，表明使用 STS 方式
                .setType("sts");
        // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }

    public ACustomsMsg orc(FileInputStream inputStream) throws Exception {
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        Client client = RecognizeTableOcr.createClient(aliYunConfig.getAccessKeyID(), aliYunConfig.getAccessKeySecret());
        // 需要安装额外的依赖库，直接点击下载完整工程即可看到所有依赖。
        RecognizeTableOcrRequest recognizeTableOcrRequest = new RecognizeTableOcrRequest()
                .setBody(inputStream);
        RuntimeOptions runtime = new RuntimeOptions();
        RecognizeTableOcrResponse resp = client.recognizeTableOcrWithOptions(recognizeTableOcrRequest, runtime);
        String jsonString = Common.toJSONString(resp);
        ACustomsMsg aCustomsMsg = ACustomsMsgORCUtil.ACustomsMsgORC(jsonString);
        return aCustomsMsg;
    }

    public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(new File("D://2.png"));
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        Client client = RecognizeTableOcr.createClient("LTAI5tF95H2ZcRfbqK9frr3f", "1T5r3au4rhYwD7MTKxsVv208DbDlAh");
        // 需要安装额外的依赖库，直接点击下载完整工程即可看到所有依赖。
        RecognizeTableOcrRequest recognizeTableOcrRequest = new RecognizeTableOcrRequest()
                .setBody(fileInputStream);
        RuntimeOptions runtime = new RuntimeOptions();
        RecognizeTableOcrResponse resp = client.recognizeTableOcrWithOptions(recognizeTableOcrRequest, runtime);
        String jsonString = Common.toJSONString(resp);
        /*数据整理*/
        ACustomsMsg aCustomsMsg = ACustomsMsgORCUtil.ACustomsMsgORC(jsonString);
        System.out.println(aCustomsMsg);
    }

}
